foregroundStyle & background
These two modifiers—foregroundStyle and background—allow you to customize the visual styling of view content and its background, supporting a wide range of styles including solid colors, gradients, materials, and dynamic appearances for light/dark mode.
foregroundStyle
Definition
Sets the style of a view’s foreground content, such as the color of text, shapes, or symbols. You can pass a single style or a layered style object (primary, secondary, tertiary) to support multi-layered rendering, such as in SF Symbols or decorated text.
Usage Examples
Basic Foreground Color
Foreground with Dynamic Colors (Light/Dark Mode)
Multi-layer Foreground Style
Use layered styles primarily with views that support multistage rendering like system icons or stylized text.
background
Definition
Sets the background of a view. You can apply simple colors or gradients, or supply a custom shape or view as the background.
Background Variants
- ShapeStyle: Use a solid color, gradient, or material.
- DynamicShapeStyle: Automatically switches styles between light and dark mode.
- Shape with Fill Style: Use a shape (e.g.,
RoundedRectangle) with a style applied to it. - VirtualNode: Use another component as the background.
- Custom Alignment: Align a background content explicitly behind the main view.
Usage Examples
Solid Color Background
Gradient Background
Dynamic Background
Background with a Shape
Background with Custom Alignment
Related Types
-
ShapeStyleA visual style that defines how a foreground or background is rendered—this can be a color, gradient, or material. Supports"red","systemBlue","#FF0000",rgba(...), and gradient definitions. -
DynamicShapeStyleA light/dark mode–aware style with separate definitions for each appearance. The system automatically applies the appropriate style based on the current UI mode. -
VirtualNodeA component used as a background, such as<Image />,<RoundedRectangle />, or any view that returns aJSX.Element. -
ShapeA predefined shape such asRoundedRectangle,Circle, orCapsule, used for styled background shapes.
Summary
These properties give you fine-grained control over visual styling and are essential for building rich, adaptive interfaces in the Scripting app.
